package chris.publishSubscribe;

import chris.ConFactory;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * EmitLog
 *
 * @author nanxin.chen
 * @since 2017/7/5
 */
public class EmitLog {

    public static final String EXCHANGE_NAME = "logs";


    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory cf = ConFactory.getConnectionFactory();
        Connection con = cf.newConnection();
        Channel channel = con.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
        String message = "log ";

        for (int i = 0; i < 10000; i++) {
            String sendStr = message + i;
            //必须要先启动consumer, 否则exchange 没有和queue绑定, 导致exchange不知道将消息发哪儿而直接丢弃
            channel.basicPublish(EXCHANGE_NAME, "", null, sendStr.getBytes());
            System.out.println(" [x] Sent '" + sendStr + "'");
        }

        channel.close();
        con.close();
    }

}
